//go:build windows

package util

import (
	"fmt"
	"os"
	"path/filepath"
	"runtime"
	"strings"
	"unsafe"

	"golang.org/x/sys/windows"
	"golang.org/x/sys/windows/registry"
)

func GetOSPrettyName() string {
	switch runtime.GOOS {
	case "windows":
		key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
		if err != nil {
			return string("获取异常")
		}
		defer func(key registry.Key) {
			err := key.Close()
			if err != nil {
				return
			}
		}(key)
		productName, _, err := key.GetStringValue("ProductName")
		if err != nil {
			return string("获取异常")
		}

		displayVersion, _, err := key.GetStringValue("DisplayVersion")
		if err != nil {
			return string("获取异常")
		}
		return fmt.Sprintf("%s %s", productName, displayVersion)
	default:
		return string("暂不支持")
	}
}

func GetInterfaceDescriptionByName(interfaceName string) string {
	return ""
}

func GetInterfaceDescriptionByMAC(mac string) string {
	var adapters [16]windows.IpAdapterInfo
	var size uint32 = uint32(unsafe.Sizeof(adapters))
	adapterList := adapters[:]

	// 获取适配器信息
	err := windows.GetAdaptersInfo(&adapterList[0], &size)
	if err != nil {
		return ""
	}

	for _, adapter := range adapterList {
		description := windows.BytePtrToString(&adapter.Description[0])
		_mac := adapter.Address[:adapter.AddressLength]
		hexStrings := make([]string, len(_mac))
		for i, v := range _mac {
			hexStrings[i] = fmt.Sprintf("%02x", v)
		}
		if strings.Join(hexStrings, ":") == mac {
			return description
		}
	}
	return ""
}

func GetConfigDirPath() string {
	exePath, err := os.Executable()
	if err != nil {
		fmt.Printf("Failed to get executable path: %v\n", err)
	}
	configDirPath := filepath.Dir(exePath) + "\\" + "config"
	return configDirPath
}

func GetNewLine() string {
	return "\r\n"
}
